package com.example.dragandview.seniorUI;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;
import com.example.dragandview.R;

public class ImageSwitcherActivity extends Activity {
    private int[] arrayImages = new int[]{R.drawable.meinv1,
            R.drawable.meinv2,R.drawable.meinv3,R.drawable.meinv4};
    private ImageSwitcher imageSwitcher;
    private int index;
    private float touchDownX,tounchUpX;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.seniorui_imageseitcher);
        //全屏显示
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

        final ImageSwitcher imageSwitcher = findViewById(R.id.imageswitcher);
        imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                ImageView imageView = new ImageView(ImageSwitcherActivity.this);
                imageView.setImageResource(arrayImages[index]);
                return imageView;
            }
        });
        imageSwitcher.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_DOWN){
                    touchDownX = event.getX();
                    return true;
                }else if(event.getAction() == MotionEvent.ACTION_UP){
                    tounchUpX = event.getX();
                    if((tounchUpX - touchDownX)>100){
                        index = index == 0 ? arrayImages.length-1 : index -1;
                        imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(ImageSwitcherActivity.this,android.R.anim.fade_out));
                        imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(ImageSwitcherActivity.this,android.R.anim.fade_in));
                        imageSwitcher.setImageResource(arrayImages[index]);
                    }else if((touchDownX - tounchUpX)>100){
                        index = index == arrayImages.length-1 ? 0 : index +1;
                        imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(ImageSwitcherActivity.this,android.R.anim.fade_out));
                        imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(ImageSwitcherActivity.this,android.R.anim.fade_in));
                        imageSwitcher.setImageResource(arrayImages[index]);
                    }
                    return true;
                }
                return false;
            }
        });
//        imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(ImageSwitcherActivity.this,android.R.anim.fade_out));
//        imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(ImageSwitcherActivity.this,android.R.anim.fade_in));
//        imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
//            @Override
//            public View makeView() {
//                ImageView imageView = new ImageView(ImageSwitcherActivity.this);
//                imageView.setImageResource(R.drawable.meinv1);
//                return imageView;
//            }
//        });
//        imageSwitcher.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                ((ImageSwitcher)v).setImageResource(R.drawable.meinv2);
//            }
//        });
    }
}
